import 'package:flutter/material.dart';

class TitleContainer extends StatelessWidget {
  final String label;
  final double? width;
  final double? height;
  final EdgeInsets? margin;
  final EdgeInsets? padding;
  final double? strokeWidth;
  final TextStyle? textStyle;
  final Color? borderColor;
  final double? space;
  final Widget? child;

  const TitleContainer(
      {super.key,
      required this.label,
      this.width,
      this.height,
      this.margin,
      this.padding,
      this.strokeWidth,
      this.textStyle,
      this.borderColor,
      this.space,
      this.child});

  @override
  Widget build(BuildContext context) {
    final labelWidth = getTextWidth(label,
        textStyle ?? TextStyle(fontSize: 14.0, color: Colors.grey.shade700));

    return Container(
        margin: margin ?? const EdgeInsets.symmetric(horizontal: 12.0),
        child: CustomPaint(
          painter: LabelBorderPainter(
              label: label,
              labelWidth: labelWidth,
              textStyle: textStyle,
              space: space ?? 4,
              strokeWidth: strokeWidth ?? 0.5,
              borderColor: borderColor ?? Colors.grey),
          child: Container(
            width: width ?? 300,
            height: height ?? 150,
            color: Colors.transparent,
            padding:
                padding ?? const EdgeInsets.only(top: 35, left: 12, right: 12),
            child: child,
          ),
        ));
  }

  double getTextWidth(String text, TextStyle style) {
    final TextPainter textPainter = TextPainter(
      text: TextSpan(text: text, style: style),
      maxLines: 1,
      textDirection: TextDirection.ltr,
    )..layout(minWidth: 0, maxWidth: double.infinity);
    return textPainter.width;
  }
}

class LabelBorderPainter extends CustomPainter {
  final String label;
  final double labelWidth;
  final double space;
  final TextStyle? textStyle;
  final Color? borderColor;
  final double strokeWidth;

  LabelBorderPainter(
      {required this.label,
      required this.labelWidth,
      required this.space,
      this.textStyle,
      this.borderColor,
      this.strokeWidth = 0.5});

  @override
  void paint(Canvas canvas, Size size) {
    final Paint paint = Paint()
      ..color = borderColor ?? Colors.grey
      ..strokeWidth = strokeWidth
      ..style = PaintingStyle.stroke;

    final textPainter = TextPainter(
      text: TextSpan(
          text: label,
          style: textStyle ??
              TextStyle(fontSize: 14.0, color: Colors.grey.shade700)),
      maxLines: 1,
      textDirection: TextDirection.ltr,
    )..layout(minWidth: 0, maxWidth: double.infinity);

    const labelX = 12.0;
    final labelY = textPainter.height;
    final h = labelY * 1.55;

    canvas.drawLine(Offset(0, h), Offset(12.0 - space, h), paint);
    canvas.drawLine(
        Offset(12.0 + labelWidth + space, h), Offset(size.width, h), paint);

    canvas.drawLine(
        Offset(size.width, h), Offset(size.width, size.height), paint);
    canvas.drawLine(
        Offset(size.width, size.height), Offset(0, size.height), paint);
    canvas.drawLine(Offset(0, size.height), Offset(0, h), paint);

    textPainter.paint(canvas, Offset(labelX, labelY));
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) => false;
}
